<?php
require_once 'CMS/Util.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php'; 
require_once 'CMS/User.php';
//Người tạo : Nguyễn Nghĩa Hưng - Team 3
//Module 	: Edit question module
//Chức năng	: Hiển thị form edit question

class QuestionAnswer_EditController extends BaseUserController{

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
	}
	
	//Chức năng show form chỉnh sửa cho người dùng
	public function editqaAction(){
		$path = APPLICATION_PATH."/widgets/questionanswer/";
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);
		
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy_content($xmlconfig->name);
		if($hasmodify=="false")
		{
			$this->view->error = 10 ;
			return;
		}
		
		$this->_helper->layout->disableLayout();
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/questionanswer');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
			$language->load($name['name']);
		}
		else
			$language->load('Default');		
		
		$languages = $tbLanguage->getAllLanguages();
		$this->view->languages = $languages;
		/*========== End Load Language ==========*/
		
		/*========== Start Get Text ==============*/
		$this->view->text_Question		= $language->get('text_Question');
		$this->view->text_Content		= $language->get('text_Content');
		$this->view->text_Comment		= $language->get('text_Comment');
		$this->view->text_Save		= $language->get('text_Save');
		$this->view->text_Edit	= $language->get('text_Edit');
		$this->view->text_Title	= $language->get('text_Title');
		$this->view->text_Cate		= $language->get('text_Cate');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		$this->view->textError3 	= $language->get('error3');
		$this->view->textError4 	= $language->get('error4');
		$this->view->textError5 	= $language->get('error5');
		$this->view->textError6 	= $language->get('error6');
		$this->view->notlogin 	= $language->get('notlog');
		$this->view->has_modify	= $language->get('has_modify');
		/*========== End Text ==============*/
		
		//Start------Khai báo các parameter
		$tbthings = new QuestionAnswer_Model_Thing();
		$tbUsers = new QuestionAnswer_Model_Users();		
		$dateInt = new Util();
		$tbcategory = new User_Model_Categories();
		//End--------Khai báo các parameter
		
		//Start------Check Input Parameter
		$session = new Zend_Session_Namespace('login.auth');
		if(!isset($session->id_user))
		{
			$this->view->error = 9;
		}
		if(isset($_SESSION["PostQA_Error"]))
		{
			$this->view->error = $_SESSION["PostQA_Error"];
			unset($_SESSION["PostQA_Error"]);
		}
		if(isset($_GET["action"])&&($_GET["action"]=="edit")&&(!isset($this->view->error)))
		{
			if(!isset($_POST["QA_category"])||(!is_numeric($_POST["QA_category"]))||($_POST["QA_category"]<=0))
			{
				$this->view->error = 3;
			}
			
			$error_desc = array();
			
			foreach($languages as $lang){
				if(!isset($_POST["QA_title"][$lang['name']])||(strlen($_POST["QA_title"][$lang['name']])<10))
				{
					$this->view->error = 2;
					$error_desc[$lang['name']][0] = $language->get('error2');
				}
			
				if(!isset($_POST["QA_description"][$lang['name']])||(strlen($_POST["QA_description"][$lang['name']])<20))
				{
					$this->view->error = 4;
					$error_desc[$lang['name']][1] = $language->get('error4');
				}
			}
			
			$this->view->error_desc = $error_desc;
			
		}
		//End--------Check Input Parameter
		
		if(count($_GET) && isset($_GET["action"]) && $_GET["action"]=="edit" &&(!isset($this->view->error)))
		{
			$qa_infos=$tbthings->getThingById($_GET["qaid"]);
			$qa_info=$qa_infos[0];
			$decription_lang=unserialize($qa_info['description']);
			$title_lang=unserialize($qa_info['title']);
			$short_des_lang=unserialize($qa_info['short_description']);
			$common_info_lang=unserialize($qa_info['common_info']);
			if(!is_array($decription_lang))
				$decription_lang["Default"]="-----------------------";
			if(!is_array($title_lang))
				$title_lang["Default"]="...";
			if(!is_array($short_des_lang))
				$short_des_lang["Default"]="-----------------------";
			if(!is_array($common_info_lang))
				$common_info_lang["Default"]="-----------------------";
			
			$tbLanguage = new Language_Model_Language();
			$all_languages= $tbLanguage->getAllLanguages();
			foreach ($all_languages as $lang)
			{
				if(!isset($decription_lang[$lang["name"]]))
					$decription_lang[$lang["name"]]="-----------------------";
				if(!isset($title_lang[$lang["name"]]))
					$title_lang[$lang["name"]]="...";
				if(!isset($short_des_lang[$lang["name"]]))
					$short_des_lang[$lang["name"]]="-----------------------";
				if(!isset($common_info_lang[$lang["name"]]))
					$common_info_lang[$lang["name"]]="-----------------------";
					
				if(isset($_POST["QA_title"][$lang["name"]]))
				{
					$title_lang[$lang["name"]]=$_POST["QA_title"][$lang["name"]];
					if($lang["language_default"]=="1")//default language
					{
						$title_lang["Default"]=$_POST["QA_title"][$lang["name"]];
					}
				}
				if(isset($_POST["QA_description"][$lang["name"]]))
				{
					$decription_lang[$lang["name"]]=$_POST["QA_description"][$lang["name"]];
					$short_des_lang[$lang["name"]]=substr($_POST["QA_description"][$lang["name"]],0,50);
					$common_info_lang[$lang["name"]]=substr($_POST["QA_description"][$lang["name"]],0,50);
					if($lang["language_default"]=="1")//default language
					{
						$decription_lang["Default"]=$_POST["QA_description"][$lang["name"]];
						$short_des_lang["Default"]=substr($_POST["QA_description"][$lang["name"]],0,50);
						$common_info_lang["Default"]=substr($_POST["QA_description"][$lang["name"]],0,50);
					}
				}
			}
			
			$updateData = array(
								'category_id'	=>	$_POST["QA_category"],
								'title'			=>	serialize($title_lang),
								'description'	=>	serialize($decription_lang),
								'common_info'	=>	serialize($common_info_lang),
								'short_description'=> serialize($short_des_lang),
								'update_date'	=>	$dateInt->get_time_now()
								);
			$result = $tbthings->updateThing($updateData, $_GET["qaid"]);
			$this->view->error = $result;
		}
		
		if(count($_GET) && isset($_GET["qaid"]) && is_numeric($_GET["qaid"]) && $_GET["qaid"]>0)
		{
			//Start------Get QA
			$questions = $tbthings->getThingById($_GET["qaid"]);
			$question = $questions[0];
			$this->view->qa_id = $question['thing_id'];
			//End--------Get QA
		}
		
		if(isset($_POST["QA_title"]))
		{
			$this->view->title = $_POST["QA_title"];
		}
		else 
		{
			$title_array=unserialize($question['title']);
			$this->view->title=$title_array;
		}
		if(isset($_POST["QA_category"]))
		{
			$this->view->category = $_POST["QA_category"];
		}
		else 
		{
			$this->view->category = $question['category_id'];
		}
		if(isset($_POST["QA_description"]))
		{
			$this->view->description = $_POST["QA_description"];
		}
		else 
		{
			$decription_array=unserialize($question['description']);
			$this->view->description= $decription_array;
		}
		
		//Start------Get QA Category
		$category=$tbcategory->getLeafCategoriesbyType(3);
		$CategoriesQA = array();
		foreach ($category as $pcate){
				$nameCate = unserialize($pcate['category_name']);
				if(isset($nameCate[$name['name']]) && isset($name)){
					$category_name = $nameCate[$name['name']];
				}else{
					$category_name = $nameCate['Default'];
				}
				$CategoriesQA[] = array(
										'category_id'	=>	$pcate['category_id'],
										'category_code'	=>	$pcate['category_code'],
										'category_name'	=>	$category_name,
										'parent_id'		=>	$pcate['parent_id'],
										'is_leaf'		=>	$pcate['is_leaf'],
										'type'			=>	$pcate['type']
									);
		}
		//End--------Get QA Category
		
		//Start------Truyền parameter qua View
		if(count($CategoriesQA)>0)
		{
			$this->view->CategoriesQA = $CategoriesQA;
		}
		else 
		{
			$this->view->CategoriesQA = array();
		}
		//End--------Truyền parameter qua View
	}
	
}